//
//  HJInformationImagesView.m
//  KaiChat
//
//  Created by lx on 2024/3/11.
//

#import "HJInformationImagesView.h"
#import "HJImageView.h"
#import "HJVideoCoverDownloader.h"

@interface HJInformationImagesView()

@property (nonatomic, strong) NSArray <HJImageView *>*imageViews;

@end

@implementation HJInformationImagesView


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        NSMutableArray *imageViews = [NSMutableArray array];
        
        for (int i = 0; i < 3; i++) {
            HJImageView *imageView = [[HJImageView alloc] init];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.addCornerRadius(10);
            imageView.image = [UIImage imageNamed:@"bigLoad"];
            [imageView addTapGesturesWithTarget:self action:@selector(tapImageView:)];
            [self addSubview:imageView];
            [imageViews addObject:imageView];
        }
        self.imageViews = imageViews;
        self.clipsToBounds = YES;
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    CGFloat margin = 10;
    CGFloat W = 0;
    
    NSArray *mediaDatas = self.images.count > 0 ? self.images : self.videos;
    
    if (mediaDatas.count < 3) {
        W = (KScreenWidth - leftRightMargin * 2 - margin) / 2;
    } else {
        W = ((KScreenWidth - leftRightMargin * 2) - (margin * 2)) / 3;
    }
    
    [self.imageViews enumerateObjectsUsingBlock:^(HJImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx < mediaDatas.count) {
            obj.frame = CGRectMake(idx * (margin + W), 0, W, self.bounds.size.height);
        } else {
            obj.frame = CGRectZero;
        }
    }];
}

- (void)setImages:(NSArray *)images {
    _images = images;
    
    [self.imageViews enumerateObjectsUsingBlock:^(HJImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx < images.count) {
            obj.hidden = NO;
            obj.showVideoFlag = NO;
            obj.anyObject = @(idx);
            NSString *url = self.images[idx];
            [obj tj_setImageWithURL:url.URL placeholderImage:[UIImage imageNamed:@"bigLoad"]];
        } else {
            obj.hidden = YES;
        }
    }];
 
    [self setNeedsLayout];
    [self layoutIfNeeded];
}

- (void)setVideos:(NSArray *)videos {
    _videos = videos;
    
    [self.imageViews enumerateObjectsUsingBlock:^(HJImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx < videos.count) {
            obj.hidden = NO;
            obj.image = [UIImage imageNamed:@"bigLoad"];
            obj.showVideoFlag = YES;
            obj.anyObject = @(idx);
            NSString *url = self.videos[idx];
            [HJVideoCoverDownloader.shareDownLoader downloadWithUrl:url completion:^(UIImage * _Nullable thumbnail) {
                obj.image = thumbnail ?: [UIImage imageNamed:@"bigLoad"];
            }];
        } else {
            obj.hidden = YES;
        }
    }];
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
}

- (void)reset {
    _videos = nil;
    _images = nil;
}

- (void)tapImageView:(UITapGestureRecognizer *)tap {
    HJImageView *imageView = (HJImageView *)tap.view;
    if (_tapImageViewBlock) {
        _tapImageViewBlock([imageView.anyObject intValue], imageView);
    }
}
 
- (void)test {
    NSLog(@"testimages = %@", self.images);
    NSLog(@"testvideos = %@", self.videos);
    [self.imageViews enumerateObjectsUsingBlock:^(HJImageView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSLog(@"testobj = %@", obj);
    }];
}
 
@end
